import gameConfig from "../../config/gameConfig";
import UserService from "../../services/userService";
import { mgrs, sers } from "../../system/OverallSystem ";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PowerBar extends cc.Component {

    @property({ type: cc.Label, displayName: "体力数" })
    private lbPowerCnt: cc.Label = null;

    @property({ type: cc.Label, displayName: "体力回复计时" })
    private lbRePowerCnt: cc.Label = null;

    onLoad() {

    }

    start() {

    }

    _init() {

    }

    OnBtnGetPower() {
        mgrs().soundMgr.playBtnSound();
        sers().uiSrv.showUserGetPowerPage();
    }

    private updatePowerCnt() {
        const power = sers().userSrv.power;
        const maxPower = gameConfig.MAX_POWER;
        let str: string = "";
        if (power > maxPower) {
            str = power.toFixed(0);
        } else {
            str = `${power}/${maxPower}`;
        }
        this.lbPowerCnt.string = str;
        if(Number(sers().localStorageSrv.get(UserService.infinityPowerModel)) != 0){
            this.lbPowerCnt.string = `MAX`;
        }
    }

    private updateRePowerTimer() {
        if (sers().userSrv.isPowerLess()) {
            this.lbRePowerCnt.node.active = true;
            const timer = sers().userSrv.getRePowerTimer();
            let time = Math.trunc(timer / 60) + ":" + Math.trunc(timer % 60);
            this.lbRePowerCnt.string = time;
        } else {
            this.lbRePowerCnt.node.active = false;
        }
    }

    update(dt) {
        this.updatePowerCnt();
        this.updateRePowerTimer();
    }
}
